<template>
  <div>
    <div class="page-form">
      <el-input
        v-model="queryParams.examScanDetails.batchNo"
        placeholder="批次"
        clearable
        @input="handleSearch"
      />
      <el-select
        v-model="queryParams.examScanDetails.examRoomNo"
        placeholder="选择考场"
        clearable
        @change="handleSearch"
      >
        <el-option
          v-for="item in kclb"
          :key="item.examRoomNo"
          :label="item.examRoomNo"
          :value="item.examRoomNo"
        ></el-option>
      </el-select>
      <el-select
        v-model="queryParams.examScanDetails.classId"
        placeholder="选择班级"
        clearable
        @change="handleSearch"
      >
        <el-option
          v-for="item in bjlb"
          :key="item.id"
          :label="item.className"
          :value="item.id"
        ></el-option>
      </el-select>
      <el-input
        v-model="queryParams.examScanDetails.admissionNo"
        placeholder="准考证号"
        clearable
        @input="handleSearch"
      />
      <el-input
        v-model="queryParams.examScanDetails.studentName"
        placeholder="姓名"
        clearable
        @input="handleSearch"
      />
    </div>
    <el-table
      stripe
      border
      ref="tableList"
      v-loading="loading"
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      row-key="id"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        :reserve-selection="true"
      />
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="批次号"
        align="center"
        prop="batchNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="流水号"
        align="center"
        prop="serialNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="准考证号"
        align="center"
        prop="admissionNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="学号"
        align="center"
        prop="examStudentId"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="姓名"
        align="center"
        prop="studentName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="学校"
        align="center"
        prop="examSchoolName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="班级"
        align="center"
        prop="examClassName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="考场号"
        align="center"
        prop="examRoomNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="座号"
        align="center"
        prop="seatNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="题卡状态"
        align="center"
        width="160"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-tag
            type="danger"
            size="mini"
            v-if="scope.row.abnormalOption == '1' && scope.row.missingExam != 1"
            >选项异常</el-tag
          >
          <el-tag size="mini" v-if="scope.row.passCard == '1'">后传题卡</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="上传时间"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ parseTime(scope.row.createTime) }}
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        width="190"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-link type="primary" @click="cktk(scope.row)">查看题卡</el-link>
          <el-link
            v-hasRole="['admin']"
            type="warning"
            v-if="scope.row.missingExam == 0"
            @click="qk(scope.row.id, 1)"
            >缺考</el-link
          >
          <el-link
            v-hasRole="['admin']"
            type="warning"
            v-if="scope.row.missingExam == 1"
            @click="qk(scope.row.id, 0)"
            >取消缺考</el-link
          >
          <el-link
            v-hasRole="['admin']"
            type="danger"
            @click="del(scope.row.id)"
            >删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="page-bottom">
      <div class="sel-box">
        <p>
          已选 <span>{{ ids.length }}</span> 项 / 共 {{ total }} 项
        </p>
        <el-button
          type="primary"
          v-if="$route.query.examSchoolId"
          @click="dc('/scan/notuploaded/record/export')"
          >导出未上传名单</el-button
        >
        <el-button
          type="primary"
          v-if="$route.query.examSchoolId"
          @click="dc('/scan/misscard/record/export')"
          >导出缺考名单</el-button
        >
        <el-button
          v-hasRole="['admin']"
          type="danger"
          :disabled="tableData.length == 0"
          @click="xxyccl"
          >选项异常处理</el-button
        >
        <el-button
          v-hasRole="['admin']"
          type="danger"
          :disabled="multiple"
          @click="del(false)"
          >批量删除</el-button
        >
        <el-button
          v-hasRole="['admin']"
          type="danger"
          @click="allDel(false)"
          >全部删除</el-button
        >
      </div>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <el-image-viewer
      v-if="imgViewerVisible"
      :on-close="closeImgViewer"
      :url-list="imgList"
    />
  </div>
</template>

<script>
import { scanRecognitionRecord, scanDel, scanAbsence,scanAlldelete } from "@/api/xmgl/smgl";
import { debounce } from "lodash";
import { checkPassword } from "@/api/common";
export default {
  components: {
    "el-image-viewer": () =>
      import("element-ui/packages/image/src/image-viewer"),
  },
  props: {
    xmxx: {},
    examSchoolId: {},
    examSubjectId: {},
    bjlb: null,
    kclb: null,
  },
  data() {
    return {
      total: 0,
      ids: [],
      tableData: [],
      multiple: true,

      loading: false,

      imgViewerVisible: false,
      imgList: [],

      queryParams: {
        pageNum: 1,
        pageSize: 15,
        examScanDetails: {
          batchNo: "",
          examRoomNo: "",
          classId: "",
          admissionNo: "",
          studentName: "",
        },
      },
      deptId: "",
    };
  },
  methods: {
    debouncedSearch: debounce(function () {
      this.queryParams.pageNum = 1;
      this.getList();
    }, 500),
    handleSearch() {
      this.debouncedSearch();
    },
    getList(page = null) {
      if (page) {
        this.queryParams.pageNum = page;
      }
      this.loading = true;
      this.tableData = [];
      scanRecognitionRecord({
        ...this.queryParams,
        examId: this.xmxx.id,
        examSubjectId: this.examSubjectId,
        examSchoolId: this.examSchoolId ? [this.examSchoolId] : null,
      })
        .then((response) => {
          this.total = response.data.total;
          this.tableData = response.data.records;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },

    xxyccl() {
      this.$router.push({
        path: "/kwgl/yccl/index",
        query: {
          examSchoolId: this.examSchoolId,
          examSubjectId: this.examSubjectId,
          templateId: this.tableData[0].templateId,
        },
      });
    },
    del(id) {
      this.$modal.confirm("请确认进行删除操作").then(() => {
        scanDel({ ids: id ? [id] : this.ids }).then((res) => {
          this.$modal.msgSuccess("操作成功");
          this.$refs.tableList.clearSelection();
          this.ids = [];
          this.getList();
        });
      });
    },
    //全部删除
    allDel(){
      // if()
      const detail = this.queryParams.examScanDetails;
      // 判断五个字段是否全部为空
      const allEmpty = Object.values(detail).every(v => !v || v === '');

      if (allEmpty) {
        this.$message.warning('请至少填写一项查询条件！');
        return;          // 拦截删除
      }
      let obj={
        examId: this.xmxx.id,
        examSubjectId: this.examSubjectId,
        examSchoolId: this.$route.query.examSchoolId,
        batchNo:this.queryParams.examScanDetails.batchNo?this.queryParams.examScanDetails.batchNo:undefined,
        examRoomNo:this.queryParams.examScanDetails.examRoomNo?this.queryParams.examScanDetails.examRoomNo:undefined,
        examClassId:this.queryParams.examScanDetails.classId?this.queryParams.examScanDetails.classId:undefined,
        admissionNo:this.queryParams.examScanDetails.admissionNo?this.queryParams.examScanDetails.admissionNo:undefined,
        studentName:this.queryParams.examScanDetails.studentName?this.queryParams.examScanDetails.studentName:undefined
      }

      this.$prompt(
        "删除考生后，可能造成考生数据异常情况，请输入管理员密码以进行下步操作!",
        "删除全部考生",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputPlaceholder: "请输入管理员密码",
          inputType: "password",
          inputValidator: (value) => {
            if (!value) {
              return "请输入密码";
            } else {
              return true;
            }
          },
        }
      )
        .then(({ value }) => {
          this.loading = true;
          checkPassword({
            password: value,
            schoolId: this.$route.query.examSchoolId
          })
            .then((res) => {
              scanAlldelete(obj).then((res) => {
                this.$modal.msgSuccess("操作成功");
                this.$refs.tableList.clearSelection();
                this.queryParams= {
                    pageNum: 1,
                    pageSize: 15,
                    examScanDetails: {
                      batchNo: "",
                      examRoomNo: "",
                      classId: "",
                      admissionNo: "",
                      studentName: "",
                    }
                  }
                this.getList();
              });
            })
            .finally(() => {
              this.loading = false;
            });
        })
        .catch(() => {});
    },
    qk(id, missingExam) {
      this.$modal
        .confirm(`请确认进行${missingExam ? "" : "取消"}缺考操作`)
        .then(() => {
          scanAbsence({ ids: [id], missingExam: missingExam, type: 2 }).then(
            (res) => {
              this.$modal.msgSuccess("操作成功");
              this.$refs.tableList.clearSelection();
              this.ids = [];
              this.getList();
            }
          );
        });
    },
    cktk(row) {
      this.imgList = [row.frontImgPath, row.backImgPath];
      this.imgViewerVisible = true;
      const m = (e) => {
        e.preventDefault();
      };
      document.body.style.overflow = "hidden";
      document.addEventListener("touchmove", m, false); // 禁止页面滑动
    },
    closeImgViewer() {
      this.imgViewerVisible = false;
      const m = (e) => {
        e.preventDefault();
      };
      document.body.style.overflow = "auto";
      document.removeEventListener("touchmove", m, true);
    },
    dc(url) {
      this.download(
        url,
        {
          examId: this.xmxx.id,
          examSchoolId: this.$route.query.examSchoolId,
        },
        ``
      );
    },
  },
};
</script>

<style>
</style>